package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;

import db.Dbclose;
import db.Dbconn;
import entity4wms.Good;


public final class DAOgood {
    static Connection conn =null;
    static Statement stmt = null;
    static ResultSet rs = null;
    static PreparedStatement pstmt = null;
    /*
     * 查询全部数据
     */
    public static void queryGoods() throws SQLException{
        var str = "SELECT * FROM goods";
        
        try {
            conn = Dbconn.getConnection();
            stmt = conn.createStatement();
            rs = stmt.executeQuery(str);
           while(rs.next()) {
                System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"
                    +rs.getString(3)+"\t"+rs.getString(4));
                
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally{
            Dbclose.queryClose(stmt,rs,conn);
        }
        
    }
    
    public static void addGoods(Good good){
        var str = "INSERT INTO goods VALUES(null,?,?,?)";
        try {
            conn = Dbconn.getConnection();
            pstmt = conn.prepareStatement(str);
            pstmt.setString(1,good.getGname() );
            pstmt.setString(2,good.getGsize() );
            pstmt.setString(3,good.getGremark() );
            pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally{
            Dbclose.close(pstmt,conn);
        }
    }

    public static void deleteGoods(Good good){
        var str = "DELETE FROM goods WHERE gid =?";
        try {
            conn = Dbconn.getConnection();
            pstmt = conn.prepareStatement(str);
            pstmt.setInt(1,good.getGid() );
            pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally{
            Dbclose.close(pstmt,conn);
        }
    }
    
    public static void updateGoods(Good good){
        var str = "UPDATE goods SET gname =?,gsize =?,gremark =? WHERE gid =?";
        try {
            conn = Dbconn.getConnection();
            pstmt = conn.prepareStatement(str);
            pstmt.setString(1,good.getGname() );
            pstmt.setString(2,good.getGsize() );
            pstmt.setString(3,good.getGremark() );
            pstmt.setInt(4,good.getGid() );
            pstmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally{
            Dbclose.close(pstmt,conn);
        }
    }
} 
